package uit.bus;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class OrderKQTKAdapter extends ArrayAdapter<OrderKQTK> {
	public ArrayList<OrderKQTK> items;
	public Context mycontext;
	public OrderKQTK o;

	public OrderKQTKAdapter(Context context, int textViewResourceId,
			ArrayList<OrderKQTK> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		items = objects;
		mycontext = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		// super.getView(position, convertView, parent);
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.row_kqtimkiem, null);
		}
		// o = new OrderKQTK();
		o = (OrderKQTK) items.get(position);
		if (o != null) {
			TextView tvkqtren = (TextView) v.findViewById(R.id.tvkqtren);
			TextView tvkqduoi = (TextView) v.findViewById(R.id.tvkqduoi);
			CheckBox cbXemtk = (CheckBox) v.findViewById(R.id.cbXemtk);
			cbXemtk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
					if (isChecked) {
						OrderKQTK o1 = (OrderKQTK) items.get(position);
						Intent i = new Intent("uit.bus.ROUTE");
						Bundle bundle = new Bundle();
						bundle.putInt("trangthai", o1.getTrangthai());
						if (o1.getTrangthai() == 1) {
							bundle.putInt("matuyen1", o1.getMatuyen1());
						} else {
							bundle.putInt("matuyen1", o1.getMatuyen1());
							bundle.putInt("matuyen2", o1.getMatuyen2());
							bundle.putString("diemchuyen", o1.getDiemchuyen());
						}
						i.putExtras(bundle);
						getContext().startActivity(i);
					}
				}
			});
			if (o.getTrangthai() == 1) {
				if (tvkqtren != null) {
					tvkqtren.setText(Html.fromHtml("<font color='#CD7F32'>"
							+ "Đi tuyến : " + "</font>"));
					tvkqtren.append(String.valueOf(o.getMatuyen1()));
				}
				if (tvkqduoi != null) {
					tvkqduoi.setText(Html.fromHtml("<font color='#CD7F32'>"
							+ o.getTentuyen1() + "</font>"));
				}
			} else {
				if (tvkqtren != null) {
					tvkqtren.setText(Html.fromHtml("<font color='#CD7F32'>"
							+ "Đi tuyến : " + "</font>"));
					tvkqtren.append(o.getMatuyen1() + " + " + o.getMatuyen2());
				}
				if (tvkqduoi != null) {
					tvkqduoi.setText(Html.fromHtml("<font color='#CD7F32'>"
							+ "Chuyển tại : " + "</font>"));
					tvkqduoi.append(o.getDiemchuyen());
				}
			}
		}
		return v;
	}
}
